package com.xxyf.servier;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xxyf.domain.User;
import com.xxyf.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @Author 小小怡飞
 * @Date 2022/11/20 2:02
 * @Version JDK 8
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    UserMapper userMapper;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUserName,username);

        User user = userMapper.selectOne(wrapper);
        if (Objects.isNull(user)){
            throw new UsernameNotFoundException("错误");
        }
        return new LoginUser(user);
    }
}
